package com.ruoyi.web.controller.trade;

import com.github.pagehelper.PageInfo;
import com.hundun.vision.api.dto.ExchangeDTO;
import com.hundun.vision.api.dto.WalletDTO;
import com.hundun.vision.api.query.WalletQuery;
import com.hundun.vision.api.request.WalletRequest;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：RuoChen
 * @date ：20:24 2021/3/2
 * @description：交易所钱包控制器
 */
@RestController
@RequestMapping("/trade/wallet")
public class WalletController extends BaseController {

    @Autowired
    private WalletService walletService;

    @PreAuthorize("@ss.hasPermi('trade:wallet:save')")
    @Log(title = "交易所钱包", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody WalletRequest request) {
        return toAjax(walletService.save(request));
    }

    @PreAuthorize("@ss.hasPermi('trade:wallet:remove')")
    @Log(title = "交易所钱包", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(walletService.removeByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('trade:wallet:update')")
    @Log(title = "交易所钱包", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult update(@RequestBody WalletRequest request) {
        return toAjax(walletService.update(request));
    }

    @PreAuthorize("@ss.hasPermi('trade:wallet:get')")
    @GetMapping(value = {"/", "/{walletId}"})
    public AjaxResult get(@PathVariable(value = "walletId", required = false) Long walletId) {
        AjaxResult ajax = AjaxResult.success();
        List<ExchangeDTO> exchanges = walletService.listExchanges();
        ajax.put("exchanges", exchanges);
        if (StringUtils.isNotNull(walletId)) {
            ajax.put(AjaxResult.DATA_TAG, walletService.getById(walletId));
        }
        return ajax;
    }

    @PreAuthorize("@ss.hasPermi('trade:wallet:search')")
    @GetMapping("/search")
    public TableDataInfo search(WalletQuery query) {
        PageInfo<WalletDTO> pageInfo = walletService.search(query);
        return getDataTable(pageInfo);
    }
}
